﻿package net.chrisjrock.ui
{
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Christopher J. Rock
	 */
	public class ConfigUi extends EventDispatcher
	{
		protected var _stage:Stage;
		protected var _controlScheme:ConfigControlScheme;
		protected var _enabled:Boolean;
		
		protected var _active:Boolean;
		protected var _muted:Boolean;
		protected var _fullScreenSourceRect:Rectangle;
		
		public static const AFFIRMATIVE:String = "affirmative";
		public static const NEGATIVE:String = "negative";
		
		public static const RESET:String = "reset";
		public static const SKIP:String = "skip";
		public static const MUTE_TOGGLE:String = "muteToggle";
		
		public function ConfigUi (stage:Stage , controlScheme:ConfigControlScheme)
		{
			_stage = stage;
			_controlScheme = controlScheme;
			_fullScreenSourceRect = new Rectangle(0 , 0 , stage.stageWidth , stage.stageHeight);
			setEnabled(true);
			_active = !stage.isFocusInaccessible();
			_stage.addEventListener(Event.DEACTIVATE , stageDeactivateHandler);
		}
		
		public function triggerAffirmative ():void
		{
			dispatchEvent(new Event(AFFIRMATIVE));
		}
		
		public function triggerNegative ():void
		{
			dispatchEvent(new Event(NEGATIVE));
		}
		
		public function triggerActiveToggle ():void
		{
			_active = !_active;
			dispatchEvent(new Event(_active ? Event.ACTIVATE : Event.DEACTIVATE));
		}
		
		public function triggerFullScreen ():void
		{
			if (_stage.displayState == StageDisplayState.NORMAL)
			{
				_stage.fullScreenSourceRect = _fullScreenSourceRect;
				_stage.displayState = StageDisplayState.FULL_SCREEN;
			}
			else
			{
				_stage.displayState = StageDisplayState.NORMAL;
			}
		}
		
		public function triggerSkip ():void
		{
			dispatchEvent(new Event(SKIP));
		}
		
		public function triggerReset ():void
		{
			dispatchEvent(new Event(RESET));
		}
		
		public function triggerControl (id:String , activation:int = 0):void
		{
			var control:KeyboardControl = _controlScheme.getControlById(id);
			if (control)
			{
				if (activation > 0)
				{
					control.activate();
				}
				else if (activation < 0)
				{
					control.deactivate();
				}
				else
				{
					control.toggle();
				}
			}
		}
		
		
		
		protected function stageDeactivateHandler (event:Event):void
		{
			if (_active)
			{
				triggerActiveToggle();
			}
		}
		
		protected function keyDownHandler (event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case _controlScheme.getAffirmative():
					triggerAffirmative();
					return;
					
				case _controlScheme.getNegative():
					triggerNegative();
					return;
					
				case _controlScheme.getActiveToggle():
					triggerActiveToggle();
					return;
					
				case _controlScheme.getFullScreenToggle():
					triggerFullScreen();
					return;
			}
			
			var control:KeyboardControl = _controlScheme.getControlByKey(event.keyCode);
			if (control)
			{
				control.activate();
			}
		}
		
		protected function keyUpHandler (event:KeyboardEvent):void
		{
			var control:KeyboardControl = _controlScheme.getControlByKey(event.keyCode);
			if (control)
			{
				control.deactivate();
			}
		}
		
		public function getControlScheme ():ConfigControlScheme
		{
			return _controlScheme;
		}
		
		public function setControlScheme (value:ConfigControlScheme):void
		{
			_controlScheme = value;
		}
		
		public function getFullScreenSourceRect ():Rectangle
		{
			return _fullScreenSourceRect;
		}
		
		public function setFullScreenSourceRect (value:Rectangle):void
		{
			_fullScreenSourceRect = value;
		}
		
		public function isMuted ():Boolean
		{
			return _muted;
		}
		
		public function setMuted (value:Boolean):void
		{
			if (_muted != value) {
				_muted = value;
				dispatchEvent(new Event(MUTE_TOGGLE));
			}
		}
		
		public function isEnabled ():Boolean
		{
			return _enabled;
		}
		
		public function setEnabled (value:Boolean):void
		{
			if (_enabled != value)
			{
				_enabled = value;
				if (_enabled)
				{
					if (KeyboardTracker.getInstance())
					{
						KeyboardTracker.getInstance().addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandler);
						KeyboardTracker.getInstance().addEventListener(KeyboardEvent.KEY_UP , keyUpHandler);
					}
				}
				else
				{
					if (KeyboardTracker.getInstance())
					{
						KeyboardTracker.getInstance().removeEventListener(KeyboardEvent.KEY_DOWN , keyDownHandler);
						KeyboardTracker.getInstance().removeEventListener(KeyboardEvent.KEY_UP , keyUpHandler);
					}
				}
			}
		}
		
		public function isActive ():Boolean
		{
			return _active;
		}
		
		public function isFullScreenEnabled ():Boolean
		{
			return _stage.displayState == StageDisplayState.FULL_SCREEN;
		}
		
		public function getStage ():Stage
		{
			return _stage;
		}
	}
	
}